package com.zc.angelica.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.angelica.entity.Account;
import com.zc.angelica.entity.ClientInfo;
import com.zc.angelica.entity.ResponseData;
import com.zc.angelica.mapper.AccountClientMapper;
import com.zc.angelica.entity.AccountClient;
import com.zc.angelica.service.AccountClientService;
import com.zc.angelica.service.ClientInfoService;
import com.zc.angelica.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 账户客户端信息(AccountClient)表服务实现类
 *
 * @author makejava
 * @since 2022-04-10 18:32:51
 */
@Service
public class AccountClientServiceImpl extends ServiceImpl<AccountClientMapper, AccountClient> implements AccountClientService {

    @Autowired
    private ClientInfoService clientInfoService;

    @Override
    public ResponseData bindingClient(String clientCode) {
        Account accountInfo = SecurityUtil.getAccountInfo();
        if (accountInfo == null) {
            return ResponseData.error("未获取到登录信息");
        }
        QueryWrapper<AccountClient> accountClientQueryWrapper = new QueryWrapper<>();
        accountClientQueryWrapper.eq("account_id", accountInfo.getId()).eq("client_code", clientCode);
        Long clientCount = this.baseMapper.selectCount(accountClientQueryWrapper);
        if (clientCount > 0) {
            return ResponseData.error("客户端已经绑定请勿重复绑定！");
        }

        QueryWrapper<ClientInfo> clientInfoQueryWrapper = new QueryWrapper<>();
        clientInfoQueryWrapper.eq("client_code", clientCode);
        ClientInfo clientInfo = clientInfoService.getOne(clientInfoQueryWrapper);
        if (clientInfo != null) {
            AccountClient accountClient = new AccountClient();
            accountClient.setAccountId(accountInfo.getId());
            accountClient.setClientCode(clientCode);
            accountClient.setClientId(clientInfo.getId());
            accountClient.setPersistentLogin(clientInfo.getPersistentLogin());
            accountClient.setUsername(accountInfo.getUsername());
            boolean save = this.save(accountClient);
            if (save) {
                return ResponseData.success("绑定成功");
            }
        }
        return ResponseData.error("绑定失败");
    }

    @Override
    public ResponseData unbindingClient(Long id) {
        boolean remove = this.removeById(id);
        if (remove) {
            return ResponseData.success("解绑成功");
        }
        return ResponseData.error("解绑失败");
    }

    @Override
    public ResponseData allClient() {
        List<ClientInfo> clientInfos = clientInfoService.list();
        Account accountInfo = SecurityUtil.getAccountInfo();
        if (accountInfo == null) {
            return ResponseData.error("未获取到登录信息");
        }
        QueryWrapper<AccountClient> accountClientQueryWrapper = new QueryWrapper<>();
        accountClientQueryWrapper.eq("account_id", accountInfo.getId());
        List<AccountClient> myClients = this.list(accountClientQueryWrapper);
        if (myClients.size() > 0) {
            List<String> myClientCodes = myClients.stream().map(AccountClient::getClientCode).collect(Collectors.toList());
            clientInfos.stream().forEach(client -> {
                if (myClientCodes.contains(client.getClientCode())) {
                    client.setBinding(2);
                }
            });
        }
        return ResponseData.success(clientInfos);
    }
}

